<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;

//前台订单控制器
class IndentController extends Controller{
	private $model;
	private $indentModel;
	public function __init(){
		//用户地址表
	    $this->model = new \Common\Model\user_address;
		//订单表
	    $this->indentModel = new \Common\Model\indent;
	}
	//默认
	public function index(){
		$sidd = Q("get.sid");
		//6.创建订单
		if(IS_POST){
			if(!$id = $this->indentModel->store()) View::error($this->indentModel->getError());
			//删除自己存入$_SESSION的商品
			foreach ($_SESSION['clearing'] as $k => $v) {
				if(in_array($k,$sidd)){
					unset($_SESSION['clearing'][$k]);
				}
			}
			//删除购物车$_SESSION的商品
			foreach ($_SESSION['cart']['goods'] as $k => $v) {
				if(in_array($k,$sidd)){
					unset($_SESSION['cart']['goods'][$k]);
				}
			}
			go(U('Successful/index',array("id"=>$id)));
		}
		//找到存在$_SESSION里面的结算商品
		$data = $_SESSION['clearing'];
		if(!$data){
			View::success('确认是否有商品提交！',U('Cart/index'));
		}
		View::with('data',$data);
		//统计总价
		$price = array();
		foreach ($data as $v) {
			$price[] = $v['total'];
		}
		//2.合计总价
		$price = array_sum($price);
		View::with('price',$price);
		//3.包裹个数
		$sid = array();
		foreach ($data as $v) {
			$sid[] = $v['num'];
		}
		$sid = count($sid);
		View::with('sid',$sid);
		//4.分配用户id
		$uid = $_SESSION['uid'];
		View::with('uid',$uid);
		//5.获得用户地址
		$addressData = $this->model->where("ua_uid={$uid}")->get();
		View::with('addressData',$addressData);
		//默认模板
	    View::make();
	}
}